import string

from module_info import *
from module_scripts import *
from patch_scripts import *

from process_common import *
from process_operations import *


# ----------------------------------------------------

def load_scripts():
    fl = open(patch_dir+"scripts.txt", "r")
    encoded = fl.readlines()
    statements = []
    target_scripts = []
    for line in encoded:
        statements += line.split()
    statements.pop(0)
    statements.pop(0)
    statements.pop(0)
    sc_n = int(statements.pop(0))
    for i in xrange(sc_n):
        cur_script = [statements.pop(0), int(statements.pop(0)), []]
        script_len = int(statements.pop(0))
        for j in xrange(script_len):
            cur_statement = [int(statements.pop(0)),]
            statement_len = int(statements.pop(0))
            for k in xrange(statement_len):
                cur_statement = cur_statement+[int(statements.pop(0)),]
            cur_script[2].append(tuple(cur_statement))
        if (cur_script[1] == -1):
            cur_script.pop(1)
        target_scripts.append(tuple(cur_script))
    return target_scripts
    

def save_updated_scripts(variable_list,variable_uses, scripts, target_scripts, updated_scripts, tag_uses,quick_strings):
    file = open(patch_dir + "scripts.txt","w")
    file.write("scriptsfile version 1\n")
    file.write("%d\n"%(len(scripts)+len(added_scripts)))
    temp_list = []
    list_type = type(temp_list)
    print "Processing native scripts..."
    for i_script in xrange(len(target_scripts)):
        func = target_scripts[i_script]
        if (len(updated_scripts)> 0) and (func[0] == scripts[updated_scripts[0]][0]):
            func = scripts[updated_scripts.pop(0)]
        if (type(func[1]) == list_type):
            file.write("%s -1\n"%(convert_to_identifier(func[0])))
            save_statement_block(file,convert_to_identifier(func[0]), 0,func[1], variable_list,variable_uses,tag_uses,quick_strings)
        else:
            file.write("%s %f\n"%(convert_to_identifier(func[0]), func[1]))
            save_statement_block(file,convert_to_identifier(func[0]), 0,func[2], variable_list,variable_uses,tag_uses,quick_strings)
        file.write("\n")
        print func[0]
    print "Processing Form Ranks scripts..."
    for i_script in added_scripts:
        func = scripts[i_script]
        if (type(func[1]) == list_type):
            file.write("%s -1\n"%(convert_to_identifier(func[0])))
            save_statement_block(file,convert_to_identifier(func[0]), 0,func[1], variable_list,variable_uses,tag_uses,quick_strings)
        else:
            file.write("%s %f\n"%(convert_to_identifier(func[0]), func[1]))
            save_statement_block(file,convert_to_identifier(func[0]), 0,func[2], variable_list,variable_uses,tag_uses,quick_strings)
        file.write("\n") 
        print func[0]       
    file.close()
    

print "Updating scripts..."
variable_uses = []
variables = load_variables(patch_dir, variable_uses)
tag_uses = load_tag_uses(patch_dir)
quick_strings = load_quick_strings(patch_dir)
target_scripts = load_scripts()
save_updated_scripts(variables,variable_uses, scripts, target_scripts, updated_scripts, tag_uses,quick_strings)
save_variables(patch_dir,variables,variable_uses)
save_tag_uses(patch_dir, tag_uses)
save_quick_strings(patch_dir,quick_strings)
